import Vue from "vue";
import Vuex from "vuex";
import axios from 'axios';
import createPersistedState from 'vuex-persistedstate'


Vue.use(Vuex);

export const store=new Vuex.Store({
    plugins: [createPersistedState()],
    state    : {
        login: false,
        token:null
    },
    getters  : {
        login:state=>{
            return state.login
        },
        logitokenn:state=>{
            return state.token
        }
    },
    mutations: {
        checkLogin:(state,payload)=>{
            state.login = payload
        },
        getToken:(state,payload)=>{
            state.token = payload
        },
    },
    
    actions: {
        checkLogin:({commit},payload)=>{
            axios({
                url   : "http://129.211.13.202:3000/"+'users/login',
                method: 'post',
                data  : {
                    name: payload.name,
                    password: payload.password
                }
            }).then(res=>{
                if(res.data.name){
                    console.log(res.data.name,"55555")
                    // commit('getToken')
                    commit('checkLogin',true)
                }else{
                    commit('checkLogin',false);
                    alert('密码错误')
                }
            }).catch(res=>{
                alert("密码错误")
            })
        },
        getToken:({commit},payload)=>{
            commit('getToken',payload)
        }
    }
})